@{layout #custom_head}

@{block #custom_head}
<link rel="stylesheet" type="text/css" href="{$basePath}/css/jquery-ui/normal/default.css" />
<link rel="stylesheet" media="screen,projection,tv" href="{$basePath}/systemApps/celebrioweather/default.css" type="text/css" />
<script type="text/javascript" src="{$basePath}/js/libs/jquery-ui.js"></script>
@{/block}

@{block #main_content}
<script type="text/javascript">
    var woeid = null;
    function updateWeatherData(){
        if(woeid === null) return;
        $.getJSON( {link weather}, { place: woeid }, function (payload) {
            $("#celebrioweatherapp").html(payload.snippets["snippet--celebrioweatherappSnippet"]);
//            try {
//                map = localStorage.getItem("weather");
//            } catch(e) {
//                map = {};
//            }
//            map[woeid] = payload.snippets["snippet--celebrioweatherappSnippet"];
//            localStorage.setItem("weather", map);
            butonize("input:submit");
        });
    }
    
    function getWeatherForCity(cityName) {
        var address ="http://where.yahooapis.com/v1/places.q("+cityName+")?appid=U7bhIjTV34GhrFPaCCvJGh59NQjwDSO2VFnZ.nUl9YazBNaL48EGTH0RWa3lzc45BgzKz8Q&format=json&callback=?";
        $.getJSON(address, function(data){         
            if(data.places.count !== 0) {
                woeid = data.places.place[0].woeid;
                updateWeatherData();
                setTimeout(function(){
                    updateWeatherData(woeid);
                }, 1800000); // every half an hour
            } else {
                if(cityName.length > 20) {
                    cityName = cityName.substring(0,20);
                    cityName = '"'+cityName+"..."+'"';
                }
                $(".celebrioweatherapp_cityNotFound").html("City "+cityName+" was not found");
            }
        });
    }
    
    function getWeatherForCurrentLocation() {
        if(navigator.onLine) {
            if(navigator.geolocation) {
                browserSupportFlag = true;
                navigator.geolocation.getCurrentPosition(function(position) {
                    $( "#progressbar" ).progressbar({ value: 1 });
                    var latlng = position.coords.latitude + "," + position.coords.longitude;
                    //var latlng = 48.899258 + "," + 18.039551;
                    var apiurl = 'http://maps.google.com/maps/geo?output=json&sensor=false&q=' + latlng;
                    $("#progressbar").progressbar("option","value",5);
                    var yqlqry = encodeURIComponent('select * from json where url="'+apiurl+'"');
                    var yqlurl = 'http://query.yahooapis.com/v1/public/yql?q='+yqlqry+'&format=json&callback=?';
                    $("#progressbar").progressbar("option","value",10);
                    $.getJSON(yqlurl, function(data) {
                        var subdata = data.query.results.json.Placemark.AddressDetails.Country.AdministrativeArea;
                        $("#progressbar").progressbar("option","value",15);
                        if (subdata.SubAdministrativeArea) {
                            cityName = subdata.SubAdministrativeArea.SubAdministrativeAreaName;
                        } else {
                            cityName = subdata.AdministrativeAreaName;
                        }
                        var address ="http://where.yahooapis.com/v1/places.q("+cityName+")?appid=U7bhIjTV34GhrFPaCCvJGh59NQjwDSO2VFnZ.nUl9YazBNaL48EGTH0RWa3lzc45BgzKz8Q&format=json&callback=?";
                        $("#progressbar").progressbar("option","value",30);
                        $.getJSON(address, function(data){
                            woeid = typeof data.places.place == "undefined" ? 818717 : data.places.place[0].woeid;
                            updateWeatherData();
                            $("#progressbar").progressbar("option","value",99);
                            //$("#progressBarParent").remove();
                            setTimeout(function(){
                                updateWeatherData(woeid);
                            }, 1800000); // every half an hour
                        });
                        $("#progressbar").progressbar("option","value",60);
                    });
                }, function() {
                    handleNoGeolocation(browserSupportFlag);
                });
            } else {
                var full_lang = {$full_lang};
                var default_city = "NY";
                switch(full_lang){
                    case "en_US":
                    case "en_EN":
                        default_city = "NY";
                        break;
                    case "cs_CZ":
                        default_city = "Prague";
                        break;
                    case "sk_SK":
                        default_city = "Bratislava";
                        break;
                    default:
                        default_city = "NY";
                        break;
                }
                var address ="http://where.yahooapis.com/v1/places.q("+default_city+")?appid=U7bhIjTV34GhrFPaCCvJGh59NQjwDSO2VFnZ.nUl9YazBNaL48EGTH0RWa3lzc45BgzKz8Q&format=json&callback=?";
                $.getJSON(address, function(data){
                    woeid = data.places.place[0].woeid;
                    updateWeatherData();
                    setTimeout(function(){
                        updateWeatherData(woeid);
                    }, 60); // every half an hour
                });
            }
        } else {
            alert('hej');
        }
    }
    $(window).load(getWeatherForCurrentLocation());
</script>

<div class="celebrioweatherapp" id="celebrioweatherapp">
    {snippet celebrioweatherappSnippet}
    <h1>{!_"Weather is loading"}</h1>
    <div id="progressBarParent"><div id="progressbar"></div></div>
    {/snippet}
    <script type="text/javascript">
        $("#celebrioweatherapp_cityFinder").live("submit", function(event){
            event.preventDefault();
            var cityName = $("#celebrioweatherapp_cityField").val();
            if(cityName !== "") {
                getWeatherForCity(cityName);
                
            }
        });
        
        $("#celebrioweatherapp_homeWeather").live("submit", function(event){
            event.preventDefault();
            getWeatherForCurrentLocation();
            
        });
    </script>
</div>
{/block}